﻿namespace Pibby.Core.IO
{
	using System.IO;
	using invrt;

	public static class DriveDiscoverer
	{		
		public static IDriveCollection ListAllRemovableDrives()
		{
			IDriveFactory factory = InversionOfController.Factory.CreateTypeOf<IDriveFactory>();

			return ListAllRemovableDrives(factory);
		}

		public static IDriveCollection ListAllRemovableDrives(IDriveFactory factory)
		{
			IDriveCollection driveList = factory.GetAllDrives();

			IDriveCollection removableDrives = new IDriveCollection();

			foreach (IDrive drive in driveList)
			{
				try
				{
					if (drive.DriveType == DriveType.Removable)
					{
						removableDrives.Add(drive);
					}
				}
				catch (System.IO.IOException)
				{
					// may be trying to connect to drives that are not ready - swallow this exception
				}
			}

			return removableDrives;
		}
	}
}
